package Ajax;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;

public class CategoryPage implements AjaxPage {
	private ResultSet rs = null;
	private String strOption = null;

	public CategoryPage(String option) {
		strOption = option;
	}

	@Override
	public void execSql(Statement stmt, HttpServletRequest request)
			throws SQLException {
		String strInfo = request.getParameter("info");
		int info = 0;

		if (strInfo != null) {
			info = Integer.parseInt(strInfo);
		}

		String sql = "SELECT list_name FROM cate_" + strOption;

		if (strOption.equalsIgnoreCase("school2") || 
			strOption.equalsIgnoreCase("departb") ||
			strOption.equalsIgnoreCase("iterb")) {
			sql += " WHERE info_id = " + info;
		}

		rs = stmt.executeQuery(sql);
	}

	@Override
	public void printXML(PrintWriter writer) throws SQLException {
		writer.print("<?xml version=\"1.0\" encoding=\"utf-8\" ?><root><![CDATA[");
		writer.print("<div class=\"caters\"><div class=\"h\"><div class=\"r\"><ol>");
		writer.print("<li><a href=\"#\" onclick=\"category.parse('0','请选择');return false;\"><img src=\"images/category_revert.gif\" alt=\"还原默认值/REVERT BE DEFAULT\" border=\"0\" /></a></li>");
		writer.print("<li><a href=\"#\" onclick=\"category.language();return false;\"><img src=\"images/category_cnlang_e.gif\" alt=\"切换语言/CHANGE LANGUAGE\" border=\"0\" /></a></li>");
		writer.print("<li><a href=\"#\" onclick=\"category.close();return false;\"><img src=\"images/category_close.gif\" alt=\"关闭选项/CLOSE OPTION\" border=\"0\" /></a></li>");
		writer.print("</ol></div>请从以下列表中选择最佳选项</div><div class=\"c\"><ol>");

		int i = 0;

		if (rs != null && rs.next()) {
			while (rs.next()) {
				String str = rs.getString(1);

				writer.println("<li><input name=\"educationradio\" type=\"radio\" value=\"0\" onclick=\"category.parse('"
						+ ++i
						+ "','"
						+ str
						+ "');category.close()\" />"
						+ str
						+ "</li>");
			}
		}

		writer.print("</ol><div class=\"clear\"></div></div></div>]]></root>");
	}

	@Override
	public void printHtml(PrintWriter writer) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void printDel(PrintWriter writer) {
		// TODO Auto-generated method stub
		
	}
}
